﻿using System;
using WeixinSDK;
using WeixinSDK.API;
using WeixinSDK.Message;

namespace WeixinDemo
{
    /// <summary>
    /// WeixinHandler 的摘要说明
    /// </summary>
    public class WxTestHandler : WxHandler
    {
        static WxTestData testData;
        public readonly static WxSetting wxSetting;

        static WxTestHandler()
        {
            testData = new WxTestData();

            wxSetting = new WxSetting()
            {
                AppId = "wxc070ee6db2cdc28d",
                AppSecret = "e3a97228c52328f8d2c00bd2f13cce23",
                UserToken = "sjlgolf",
                TokenCacheFolder = AppDomain.CurrentDomain.BaseDirectory
            };
        }

        public WxTestHandler()
            : base(wxSetting)
        {

        }

        protected override string ProcessRecMessage_Text(WxMsg msg)
        {
            var content = msg[WxMsgField.Content];

            if (content != null)
            {
                switch (msg[WxMsgField.Content].ToLowerInvariant())
                {
                    case "我的信息":
                        var userInfo = UserManager.GetUserInfo(AccessToken.Get(wxSetting), msg.FromUserName);
                        if (userInfo.HasError)
                        {
                            return BuildResponeXml(msg.FromUserName, msg.ToUserName, "错误:" + userInfo.errmsg);
                        }
                        return BuildResponeXml(msg.FromUserName, msg.ToUserName,
                            string.Format("性别:{0},昵称:{1}", userInfo.sex, userInfo.nickname));
                    case "music":
                    case "音乐":
                        return BuildResponeXml(msg.FromUserName, msg.ToUserName,
                            new ResMusic(testData.VoiceMediaId)
                            {
                                Title = "好听的音乐",
                                Description = "你刚才的声音",
                                HQMusicUrl = "http://zhangmenshiting.baidu.com/data2/music/108215782/14385500158400128.mp3?xcode=f85384299851f8efe153df6742316752cc816ddc4bb4461a",
                                MusicURL = "http://zhangmenshiting.baidu.com/data2/music/108215782/14385500158400128.mp3?xcode=f85384299851f8efe153df6742316752cc816ddc4bb4461a"
                            });
                    case "image":
                    case "图片":
                        return BuildResponeXml(msg.FromUserName, msg.ToUserName, new ResImage(testData.ImageMediaId));
                    case "video":
                    case "视频":
                        return BuildResponeXml(msg.FromUserName, msg.ToUserName,
                            new ResVideo(testData.VideoMediaId)
                            {
                                Title = "好看的视频",
                                Description = "这只是一段演示用的视频"
                            });
                    case "news":
                    case "图文":
                        return BuildResponeXml(msg.FromUserName, msg.ToUserName,
                           new ResNews()
                           {
                               new ResArtical(){ Title="第一条图文",Description="你看你看",
                                   PicUrl="http://like100.com/weixin/test.jpg",
                                   Url="http://www.baidu.com/"
                               },
                                new ResArtical(){ Title="第二条图文",Description="我看我看",
                                   PicUrl="http://like100.com/weixin/test.jpg",
                                   Url="http://www.baidu.com/"
                               }
                           });
                }
            }

            return base.ProcessRecMessage_Text(msg);
        }

        protected override string ProcessRecMessage_Image(WxMsg msg)
        {
            testData.ImageMediaId = msg[WxMsgField.MediaId];

            //var accessToken = Wx.AccessToken.Get(wxSetting);

            //var wxErr = Wx.MediaFile.Download(accessToken, msg[WxMsgField.MediaId],
            //    System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, testData.ImageMediaId + ".jpg"));

            //if (wxErr != null)
            //{
            //    _logger.Error(wxErr.ToString());
            //}

            return base.ProcessRecMessage_Image(msg);
        }

        protected override string ProcessRecMessage_Voice(WxMsg msg)
        {
            testData.VoiceMediaId = msg[WxMsgField.MediaId];
            return base.ProcessRecMessage_Voice(msg);
        }

        protected override string ProcessRecMessage_Video(WxMsg msg)
        {
            testData.VideoMediaId = msg[WxMsgField.ThumbMediaId];
            return base.ProcessRecMessage_Video(msg);
        }
    }

    class WxTestData
    {
        public string ImageMediaId { get; set; }
        public string VoiceMediaId { get; set; }
        public string VideoMediaId { get; set; }
    }
}